package com.orangelife.webservice.service.propinfo.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.orangelife.common.AbstractV2TransferService;
import com.orangelife.exception.AppBizException;
import com.orangelife.framework.toolkit.validate.ValidateUtil;
import com.orangelife.service.cerHouseholder.ICerHouseholderService;
import com.orangelife.service.propinfo.IPropInfoService;
import com.orangelife.service.propinfo.model.KlCmmApartments;
import com.orangelife.service.propinfo.model.KlCmmBuilding;
import com.orangelife.service.propinfo.model.KlCmmUnit;
import com.orangelife.service.propinfo.model.PropFeeApartment;
import com.orangelife.service.propinfo.model.PropFeeBuilding;
import com.orangelife.service.propinfo.model.PropFeeUnit;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.service.propinfo.IPropInfoWebService;

@Service("PropInfoWebService")
public class PropInfoWebServiceImpl extends AbstractV2TransferService implements
		IPropInfoWebService {

	@Resource(name = "propInfoService")
	private IPropInfoService propInfoService;
	@Resource(name = "cerHouseholderService")
	private ICerHouseholderService cerHouseholderService;

	@Override
	public Response moveDataByCommId(HttpServletRequest servletRequest,
			String commId) throws Exception, AppBizException {

		/* commID为空的场合 */
		if (StringUtils.isBlank(commId)) {
			return this.abnormalResp(100002, "小区ID不能为空");

		}
		/* commID格式错误的场合 */
		if (!ValidateUtil.checkUUID(commId)) {
			logger.error(InfoConst.SYS_INFO_1003, "小区ID", commId);
			return this.abnormalResp(CodeConst.SYS_CODE_1003,
					InfoConst.SYS_INFO_1003, "小区ID", commId);
		}

		List<KlCmmBuilding> klCmmBuildings = propInfoService
				.selectBuildingsByCommId(commId);
		List<String> buildingIds = new ArrayList<String>();
		for (KlCmmBuilding klCmmBuilding : klCmmBuildings) {
			PropFeeBuilding propFeeBuilding = new PropFeeBuilding();

			BeanUtils.copyProperties(klCmmBuilding, propFeeBuilding);
			buildingIds.add(klCmmBuilding.getBuildingId());
			try {
				propInfoService.insertBuildingSelective(propFeeBuilding);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		List<KlCmmUnit> klCmmUnits = propInfoService
				.selectUnitsByBuildingIdList(buildingIds);

		List<KlCmmApartments> klCmmApartments = propInfoService
				.selectApartmentsByBuildingIdList(buildingIds);

		for (KlCmmUnit klCmmUnit : klCmmUnits) {
			PropFeeUnit propFeeUnit = new PropFeeUnit();
			BeanUtils.copyProperties(klCmmUnit, propFeeUnit);
			try {
				propInfoService.insertUnitSelective(propFeeUnit);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		for (KlCmmApartments klCmmApartment : klCmmApartments) {

			PropFeeApartment propFeeApartment = new PropFeeApartment();
			BeanUtils.copyProperties(klCmmApartment, propFeeApartment);
			propFeeApartment.setApartmentNum(klCmmApartment.getApartmentNumb());
			try {
				propInfoService.insertApartmentSelective(propFeeApartment);
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}

		}

		return this.normalResp();
	}

	@Override
	public Response getAllInfo(HttpServletRequest servletRequest, String commID)
			throws Exception, AppBizException {

		List<Map<String, Object>> list = propInfoService.getAllInfo(commID);

		return this.normalResp(0, 0, 0l, list);
	}

	@Override
	public Response deleteOwners(HttpServletRequest servletRequest,
			String linkAptHouseholderID) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {
			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (StringUtils.isBlank(linkAptHouseholderID)) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}
			cerHouseholderService
					.deleteCerHouseholderBylinkID(linkAptHouseholderID);
			logger.info("在" + new Date() + "删除授权ID为" + linkAptHouseholderID
					+ "的授权信息(消息体  )");
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response addOwners(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {
			cerHouseholderService.addOwners(paMap);
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response editOwners(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {
			cerHouseholderService.editOwners(paMap);
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response addProperty(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.addProperty(paMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response editProperty(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.editProperty(paMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response importProperty(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.importProperty(paMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response changeOwner(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.changeOwner(paMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response getOwners(HttpServletRequest servletRequest,
			String apartmentID) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			map.put("pageEntity", cerHouseholderService
					.findCerHouseholderByRelevantID(null, apartmentID, null,
							null, null, null, null));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response editComm(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.editComm(paMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response addBuilding(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.addBuilding(paMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response editBuilding(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.editBuilding(paMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response deletBuilding(HttpServletRequest servletRequest,
			String buildingID) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.deleteBuilding(buildingID);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response addUnit(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.addUnit(paMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response editUnit(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.editUnit(paMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response deletUnit(HttpServletRequest servletRequest, String unitID)
			throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.deleteUnit(unitID);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response addApartment(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.addApartment(paMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response editApartment(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.editApartment(paMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response deletApartment(HttpServletRequest servletRequest,
			String apartmentID) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.deleteApartment(apartmentID);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response bauthEditBuildingUnit(HttpServletRequest servletRequest,
			Map<String, Object> parMap) throws Exception {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.bauthEditBuildingUnit(parMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response bauthEditUnitApart(HttpServletRequest servletRequest,
			Map<String, Object> parMap) throws Exception {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.bauthEditUnitApart(parMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response bauthDeleteBuilding(HttpServletRequest servletRequest,
			Map<String, Object> parMap) throws Exception {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.bauthDeleteBuilding(parMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response bauthDeleteUnit(HttpServletRequest servletRequest,
			Map<String, Object> parMap) throws Exception {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.bauthDeleteUnit(parMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response bauthDeleteApartment(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.bauthDeleteApartment(paMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response addComm(HttpServletRequest servletRequest,
			Map<String, Object> paMap) throws Exception, AppBizException {// linkAptHouseholderID

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			cerHouseholderService.addComm(paMap);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response getBuildings(HttpServletRequest servletRequest,
			String commID) throws Exception, AppBizException {
	
		List<Map<String, Object>> list = propInfoService.getBuilding(commID);

		return this.normalResp(0, 0, 0l, list);
		
	}

	@Override
	public Response getUnits(HttpServletRequest servletRequest,
			String buildingID) throws Exception, AppBizException {
		

		List<Map<String, Object>> list = propInfoService.getUnits(buildingID);

		return this.normalResp(0, 0, 0l, list);
	}

	@Override
	public Response getApartmentsByUnitID(HttpServletRequest servletRequest,
			String unitID) throws Exception, AppBizException {
		List<Map<String, Object>> list = propInfoService.getApartmentsByBuilding(null, unitID);

		return this.normalResp(0, 0, 0l, list);
	}

	@Override
	public Response getApartmentsByBuiLding(HttpServletRequest servletRequest,
			String buildingID) throws Exception, AppBizException {
		
		List<Map<String, Object>> list = propInfoService.getApartmentsByBuilding(buildingID, null);

		return this.normalResp(0, 0, 0l, list);
	}

	@Override
	public Response getApartments(HttpServletRequest servletRequest,
			Map<String, Object> params) throws Exception, AppBizException {
		
		if(params.get("commID")==null||StringUtils.isBlank(params.get("commID").toString())){
			
			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区ID不能为空");
			
		}
	    if(params.get("apartmentNum")==null||StringUtils.isBlank(params.get("apartmentNum").toString())){
			
			return this.abnormalResp(CodeConst.SYS_CODE_1001, "房号不能为空");
			
		}
	
		if(params.get("pageIndex")==null){
			params.put("pageIndex", 1)	;
		}
		
		if(params.get("pageSize")==null){
			params.put("pageSize", 1000000)	;
		}
		List<Map<String, Object>> list = propInfoService.getApartments(params);
		Long total = ((Page<Map<String, Object>>) list).getTotal();
		
		return this.normalResp(Integer.parseInt(params.get("pageIndex").toString()), Integer.parseInt(params.get("pageSize").toString()), total, list);
	}

	@Override
	public Response getOwners(HttpServletRequest servletRequest,
			Map<String, Object> params) throws Exception, AppBizException {
		
		
        if(params.get("commID")==null||StringUtils.isBlank(params.get("commID").toString())){
			
			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区ID不能为空");
			
		}
	    if(params.get("ownerName")==null||StringUtils.isBlank(params.get("ownerName").toString())){
			
			return this.abnormalResp(CodeConst.SYS_CODE_1001, "业主名称不能为空");
			
		}
	    
	    if(params.get("pageIndex")==null){
			params.put("pageIndex", 1)	;
		}
		
		if(params.get("pageSize")==null){
			params.put("pageSize", 1000000)	;
		}
		List<Map<String, Object>> list = propInfoService.getOwners(params);
		Long total = ((Page<Map<String, Object>>) list).getTotal();

		return this.normalResp(Integer.parseInt(params.get("pageIndex").toString()), Integer.parseInt(params.get("pageSize").toString()), total, list);
	}
}
